﻿using Management.BLL;
using Management.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MET.Web.Controllers
{
    public class UsersController : Controller
    {
        //
        // GET: /Users/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetAll()
        {
            UserService service = new UserService();
            var v = service.GetAllUsers();
            return Json(v);
        }

        [HttpPost]
        [UIExceptionResult]
        public ActionResult Add(User entity)
        {
            UserService service = new UserService();
            string userIDs = HttpContext.Request["userRoles"];
            string sexValue = HttpContext.Request["sexValue"];
            //entity.Sex = sexValue == "true";
            entity.CreateUserID = new CurrentUserHelper().UserID;
            entity.CreateDate = DateTime.Now;
            entity.IsVisible = true;
            bool status = service.Insert(entity, userIDs);
            return this.Content("OK");
            //if (entity != null && !string.IsNullOrWhiteSpace(entity.UserPassword) && !string.IsNullOrWhiteSpace(entity.UserPassword))
            //{
            //    UserService service = new UserService();
            //    var result = service.Insert(entity);
            //    if (result)
            //        return Content("OK");
            //    else
            //        return Content("ERROR");
            //}
            //else
            //    return Content("ERROR");
        }
        [UIExceptionResult]
        public ActionResult GetInfo(int id)
        {
            UserService service = new UserService();
            var V = service.GetUsersInfo(id);
            return Json(V, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Update(User entity)
        {
            UserService service = new UserService();
            var v = service.Update(entity);
            if (v)
            {
                return Content("OK");
            }
            return Content("ERROR");
        }
        [HttpPost]
        public ActionResult Delete(int id)
        {
            UserService service = new UserService();
            var entity = service.GetUsersInfo(id);
            var v = service.Delete(entity);
            if (v)
            {
                return Content("OK");
            }
            return Content("ERROR");
        }
    }
}
